﻿// <ParaBellum for logging mobile worker.>
//     Copyright (C) 2013  APKO
// 
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Windows;
using ParaBellum.Properties;

namespace ParaBellum {
    internal class UserPreferences {
        #region Member Variables

        private double _windowHeight;
        private double _windowLeft;
        private WindowState _windowState;
        private double _windowTop;
        private double _windowWidth;

        #endregion //Member Variables

        #region Public Properties

        public double WindowTop {
            get { return this._windowTop; }
            set { this._windowTop = value; }
        }

        public double WindowLeft {
            get { return this._windowLeft; }
            set { this._windowLeft = value; }
        }

        public double WindowHeight {
            get { return this._windowHeight; }
            set { this._windowHeight = value; }
        }

        public double WindowWidth {
            get { return this._windowWidth; }
            set { this._windowWidth = value; }
        }

        public WindowState WindowState {
            get { return this._windowState; }
            set { this._windowState = value; }
        }

        #endregion //Public Properties

        #region Constructor

        public UserPreferences() {
            //Load the settings
            this.Load();
            //Size it to fit the current screen
            this.SizeToFit();
            //Move the window at least partially into view
            this.MoveIntoView();
        }

        #endregion //Constructor

        #region Functions

        /// <summary>
        ///     If the saved window dimensions are larger than the current screen shrink the
        ///     window to fit.
        /// </summary>
        public void SizeToFit() {
            if (this._windowHeight > SystemParameters.VirtualScreenHeight) {
                this._windowHeight = SystemParameters.VirtualScreenHeight;
            }
            if (this._windowWidth > SystemParameters.VirtualScreenWidth) {
                this._windowWidth = SystemParameters.VirtualScreenWidth;
            }
        }

        /// <summary>
        ///     If the window is more than half off of the screen move it up and to the left
        ///     so half the height and half the width are visible.
        /// </summary>
        public void MoveIntoView() {
            if (this._windowTop + this._windowHeight/2 > SystemParameters.VirtualScreenHeight) {
                this._windowTop = SystemParameters.VirtualScreenHeight - this._windowHeight;
            }
            if (this._windowLeft + this._windowWidth/2 > SystemParameters.VirtualScreenWidth) {
                this._windowLeft = SystemParameters.VirtualScreenWidth - this._windowWidth;
            }
            if (this._windowTop < 0) {
                this._windowTop = 0;
            }
            if (this._windowLeft < 0) {
                this._windowLeft = 0;
            }
        }

        private void Load() {
            this._windowTop = Settings.Default.WindowTop;
            this._windowLeft = Settings.Default.WindowLeft;
            this._windowHeight = Settings.Default.WindowHeight;
            this._windowWidth = Settings.Default.WindowWidth;
            this._windowState = Settings.Default.WindowState;
        }

        public void Save() {
            if (this._windowState != WindowState.Minimized) {
                Settings.Default.WindowTop = this._windowTop;
                Settings.Default.WindowLeft = this._windowLeft;
                Settings.Default.WindowHeight = this._windowHeight;
                Settings.Default.WindowWidth = this._windowWidth;
                Settings.Default.WindowState = this._windowState;
                Settings.Default.Save();
            }
        }

        #endregion //Functions
    }
}